﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;

/// <summary>
/// TUser 的摘要说明
/// </summary>
public class TUser : TData
{
    //常量
    const string CTableName = "t_user";
    const string CKeyName = "uid";
    //与数据表字段对应的变量
    private string FUID;
    private string FEMail;
    private string FBigName;
    private string FPassWord;
    private string FUserName;
    private string FMyName;
    private string FMySex;
    private string FMyCompany;
    private string FMyDept;
    private string FMyPic;
    private string FMyIntro;
    private int FStatus;
    private DateTime FRegTime;
    private int FIId;

    //分页显示使用
    public System.Data.SqlClient.SqlDataReader RResults;
    public int RowCounts;
    public int PageCounts;
    const int PageSize = 40;
    
    public TUser()
	{
        FTableName = CTableName;
        FKeyName = CKeyName;
    }

    public TUser(string sid)
    {
        FTableName = CTableName;
        FKeyName = CKeyName;
        FKeyValue = sid;
    }

    //根据uid获得记录值
    public override void Select()
    {
        string strSql = GetSelectString();
        TSqlDB oDB = new TSqlDB(strSql);
        if (!oDB.EOF)
        {
            FUID = oDB["uid"].ToString();
            FEMail = oDB["email"].ToString();
            FBigName = oDB["bigname"].ToString();
            FPassWord = oDB["password"].ToString();
            FUserName = oDB["usrname"].ToString();
            FMyName = oDB["myname"].ToString();
            FMySex = oDB["mysex"].ToString();
            FMyCompany = oDB["mycompany"].ToString();
            FMyDept = oDB["mydept"].ToString();
            FMyPic = oDB["mypic"].ToString();
            FMyIntro = oDB["myintro"].ToString();
            FStatus = oDB["status"].ToInt();
            FRegTime = oDB["regtime"].ToDateTime();
            FIId = oDB["iid"].ToInt();
        }
        oDB.Free();
    }

    //插入新记录。
    public void Insert()
    {
        string ExSql = "newid(),'" + FEMail + "','" + FBigName + "','" + FPassWord + "','" + FUserName + "','" + FMyName + "','"+FMySex+"','"+FMyCompany+"','"+FMyDept+"','"+FMyPic+"','"+FMyIntro+"',";
        ExSql += FStatus.ToString() + ",getdate()";
        base.Insert(ExSql);
    }

    //更新记录。
    public void Update()
    {
        string ExSql = "bigname='" + FBigName + "',username='" + FUserName + "',myname='" + FMyName + "',mysex='" + FMySex + "',mycompany='"+FMyCompany+"',mydept='"+FMyDept+"',myintro='" + FMyIntro + "'";
        base.Update(ExSql);
    }

    //更新记录。
    public void Update2()
    {
        string ExSql = "bigname='" + FBigName + "',myintro='" + FMyIntro + "'";
        base.Update(ExSql);
    }

    //更新头像。
    public void Update_pic()
    {
        string ExSql = "mypic='" + FMyPic + "'";
        base.Update(ExSql);
    }

    //更新email。
    public void Update_email()
    {
        string ExSql = "email='" + FEMail + "'";
        base.Update(ExSql);
    }

    //改变状态,0=未验证,1=已通过验证
    public void Update_status()
    {
        string ExSql = "status=" + FStatus.ToString(); ;
        base.Update(ExSql);
    }

    //静态，可以不用创建对象即可调用。
    public static void DeleteByID(string sID)
    {
        TData.DeleteByID(sID, CTableName, CKeyName);
    }

    //更改密码
    public void ChangePWD(string sPWD)
    {
        string ExSql = "password='" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sPWD, "MD5") + "'";
        base.Update(ExSql);
    }

    /// <summary>
    /// （静态）私有：根据email和密码检查用户身份
    /// email和密码正确则返回email，否则返回“”
    /// </summary>
    private static string CheckLoginReturnEmail(string email, string password)
    {
        email = email.Trim();
        password = password.Trim();

        string Result = "";
        bool isValid = false;

        // 验证数据库用户，并取email
        string sSql = "select email from t_user where status=1 and email='" + email + "' and password='" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5") + "'";
        TSqlDB oUser = new TSqlDB(sSql);
        isValid = !oUser.EOF;
        if (isValid)
            Result = oUser["email"].ToString();
        oUser.Free();

        // 返回结果
        return Result;
    }

    /// <summary>
    /// （静态）根据email和密码检查用户身份
    /// email和密码正确则返回“真”，否则返回“假”
    /// </summary>
    public static bool CheckLogin(string email, string password)
    {
        bool Result = false;
        string EMail = CheckLoginReturnEmail(email, password);
        Result = EMail.Length > 0;

        // 返回结果
        return Result;
    }

    /// <summary>
    /// （静态）根据email和密码检查用户身份
    /// email和密码正确则返回“真”，否则返回“假”，并将email保存在Session中
    /// </summary>
    public static bool CheckLogin(string email, string password, System.Web.UI.Page page)
    {
        bool Result = false;
        string EMail = CheckLoginReturnEmail(email, password);
        Result = EMail.Length > 0;

        // 保存Session
        if (Result)
        {
            string sSql = "select uid from t_user where email='" + EMail + "'";
            TSqlDBReader oReader = new TSqlDBReader(sSql);
            oReader.FDataReader.Read();
           
            page.Session["ecrm_uid"] = oReader.FDataReader.GetValue(0).ToString();
            oReader.Free();

            page.Session["ecrm_email"] = EMail;
        }
        // 返回结果
        return Result;
    }

    /// <summary>
    ///（静态）检查用户是否是合法用户
    /// </summary>
    public static void CheckUser(System.Web.UI.Page page)
    {
        if ((page.Session["ecrm_email"] == "") || (page.Session["ecrm_email"] == null))
        {
            page.Server.Transfer("\\easycrm\\user\\user_login.aspx");
        }
    }

    //分页显示
    public void GetPage(int PageNum)
    {
        int iStartRow, iStopRow;

        iStartRow = (PageNum - 1) * PageSize + 1;
        iStopRow = PageNum * PageSize;

        TSqlDBProc oProc = new TSqlDBProc("ms_GetLinkmanPage", 0);
        oProc.ms_GetLinkmanPage(iStartRow, iStopRow, FUID);

        RResults = oProc.FDataReader;
    }
    
    //生成pagebar
    public string GenPagebar(int PageNum)
    {
        string result = "<div class=\"pagebox\">";

        CountPage();

        if (PageCounts > 1)
        {
            if (PageNum == 1)//第一页
            {
                result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
                result += "<span class=\"pagebox_next\"><a onclick=\"getlinkmans('2')\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('" + PageCounts.ToString() + "')\">尾页</a></span>";
            }
            if ((PageNum > 1) && (PageNum < PageCounts))
            {
                result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('1')\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a onclick=\"getlinkmans('" + (PageNum - 1).ToString() + "')\">上一页</a></span>";
                result += "<span class=\"pagebox_next\"><a onclick=\"getlinkmans('" + (PageNum + 1).ToString() + "')\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('" + PageCounts.ToString() + "')\">尾页</a></span>";
            }

            if (PageNum == PageCounts)//最后一页
            {
                result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('1')\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a onclick=\"getlinkmans('" + i.ToString() + "')\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a onclick=\"getlinkmans('" + (PageCounts - 1).ToString() + "')\">上一页</a></span>";
                result += "<span class=\"pagebox_next_nolink\">下一页</span>";
                result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
            }
        }
        else
        {
            result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
            result += "<span class=\"pagebox_num_nonce\"><a>1</a></span>";
            result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
            result += "<span class=\"pagebox_next_nolink\">下一页</span>";
            result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
        }

        result += "<span style=\"padding-top: 4px;\">&nbsp;(每页显示" + PageSize.ToString() + "行)</span></div>";

        return result;
    }
    
    //计算页数
    private void CountPage()
    {
        string sSql = "select count(*) from t_linkman where inman='" + FUID + "'";
        RowCounts = TSqlDB.ExecuteScalar(sSql);

        if (RowCounts % PageSize == 0)  //余数为零
        {
            PageCounts = RowCounts / PageSize;
        }
        else
        {
            PageCounts = (int)(RowCounts / PageSize) + 1;
        }
    }

    public string UID
    {
        get
        {
            return FUID;
        }
        set
        {
            FUID = value;
        }
    }

    public string EMail
    {
        get
        {
            return FEMail;
        }
        set
        {
            FEMail = value;
        }
    }

    public string BigName
    {
        get
        {
            return FBigName;
        }
        set
        {
            FBigName = value;
        }
    }

    public string PassWord
    {
        get
        {
            return FPassWord;
        }
        set
        {
            FPassWord = value;
        }
    }

    public string UserName
    {
        get
        {
            return FUserName;
        }
        set
        {
            FUserName = value;
        }
    }

    public string MyName
    {
        get
        {
            return FMyName;
        }
        set
        {
            FMyName = value;
        }
    }

    public string MySex
    {
        get
        {
            return FMySex;
        }
        set
        {
            FMySex = value;
        }
    }

    public string MyCompany
    {
        get
        {
            return FMyCompany;
        }
        set
        {
            FMyCompany = value;
        }
    }

    public string MyDept
    {
        get
        {
            return FMyDept;
        }
        set
        {
            FMyDept = value;
        }
    }

    public string MyPic
    {
        get
        {
            return FMyPic;
        }
        set
        {
            FMyPic = value;
        }
    }

    public string MyIntro
    {
        get
        {
            return FMyIntro;
        }
        set
        {
            FMyIntro = value;
        }
    }

    public int Status
    {
        get
        {
            return FStatus;
        }
        set
        {
            FStatus = value;
        }
    }

    public DateTime RegTime
    {
        get
        {
            return FRegTime;
        }
        set
        {
            FRegTime = value;
        }
    }

    public int IId
    {
        get
        {
            return FIId;
        }
        set
        {
            FIId = value;
        }
    }
}
